<template>
  <div class="login" :style="{height:loginHeight}">
    <div class="title-bg">
      智慧农业物联网平台
    </div>
    <div class="userinfo">
      <div style="margin-top:160px;margin-left:80px;">
        <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" autocomplete="on" label-position="left">
          <el-form-item prop="username">
            <el-input

              ref="username"
              v-model="loginForm.username"
              placeholder="Username"
              name="username"
              type="text"
              tabindex="1"
              autocomplete="on"
            />
          </el-form-item>

          <el-tooltip v-model="capsTooltip" content="Caps lock is On" placement="right" manual>
            <el-form-item prop="password">
              <el-input
                :key="passwordType"
                ref="password"
                v-model="loginForm.password"
                :type="passwordType"
                placeholder="Password"
                name="password"
                tabindex="2"
                autocomplete="on"
                @keyup.native="checkCapslock"
                @blur="capsTooltip = false"
                @keyup.enter.native="handleLogin"
              />
              <span class="show-pwd" @click="showPwd">
                <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
              </span>
            </el-form-item>
          </el-tooltip>
        </el-form>
        <button class="submit-button" @click="handleLogin">登录</button>
      </div>
    </div>

  </div>
</template>

<script>
import { validUsername } from '@/utils/validate'
import SocialSign from './components/SocialSignin'
export default {
  name: 'Login',
  components: { SocialSign },
  data() {
    const validateUsername = (rule, value, callback) => {
      if (!validUsername(value)) {
        callback(new Error('Please enter the correct user name'))
      } else {
        callback()
      }
    }
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error('The password can not be less than 6 digits'))
      } else {
        callback()
      }
    }
    return {
      loginHeight: window.innerHeight + 'px',
      loginForm: {
        username: '',
        password: ''
      },
      loginRules: {
        username: [{ required: true, trigger: 'blur', validator: validateUsername }],
        password: [{ required: true, trigger: 'blur', validator: validatePassword }]
      },
      passwordType: 'password',
      capsTooltip: false,
      loading: false,
      showDialog: false,
      redirect: undefined,
      otherQuery: {}
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        const query = route.query
        /*
        if (query) {
          this.redirect = query.redirect
          this.otherQuery = this.getOtherQuery(query)
        }
        */
      },
      immediate: true
    }
  },
  created() {
    // window.addEventListener('storage', this.afterQRScan)
  },
  mounted() {
    if (this.loginForm.username === '') {
      this.$refs.username.focus()
    } else if (this.loginForm.password === '') {
      this.$refs.password.focus()
    }
  },
  destroyed() {
    // window.removeEventListener('storage', this.afterQRScan)
  },
  methods: {
    checkCapslock(e) {
      const { key } = e
      this.capsTooltip = key && key.length === 1 && (key >= 'A' && key <= 'Z')
    },
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          this.$store.dispatch('user/login', this.loginForm)
            .then(() => {
              console.log('okkk')
              console.log(this.$store.getters.permission_routes)
              this.$router.push({ path: '/index' })

              this.$store.dispatch('permission/generateRoutes', this.$store.getters.roles)

              this.loading = false
            })
            .catch(() => {
              this.loading = false
            })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.login{
    width:100%;
    background:url('../../assets/images/background2.png');
    background-size:100% 100%;
    /* background-repeat:repeat-y; */
  }
  .title-bg{
    width:550px;
    height:80px;
    margin:0 auto;
    background:url('../../assets/images/title-bg.png');
    background-repeat:no-repeat;
    background-size:100%;
    color:#00F0FF;
    font-size:38px;
    font-family:RTWSShangYaG0v1-Regular;
    text-align:center;
    line-height:80px;
  }
  .userinfo{
    float:right;
    margin-right:100px;
    margin-top:50px;
    width:500px;
    height:450px;
    background:url('../../assets/images/userinfo-bg.png');
    background-repeat:no-repeat;
    background-size:100%;
  }
  .submit-button{width:350px;height:40px;border-radius:3px;background:#37A2B6;border:solid 2px #37A2B6;color:white;font-size:18px;margin-top:20px;cursor:pointer;}
  .submit-button:hover{background:#00E3EF;}
  .el-input{
    width:350px;height:40px;border-radius:3px;background:transparent ;border:solid 1px #37A2B6;color:white;font-size:18px;margin-top:20px;
  }
  .el-input__inner{
      background: transparent;
  }
</style>
